<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

/**
用户控制器操作类
数据库模型参考文档https://blog.csdn.net/weixin_44535476/article/details/89074175
*/
class UserController extends Controller
{
    /**
     * @return Response
     */
    public function index()
    {
        $users = DB::select('select * from users where status = ?', [1]); //按条件查询
        print_r($users);
        //return view('user.index', ['users' => $users]); //调取页面，返回数据
        return view('user/index', ['users' => $users]); //调取页面，返回数据
    
    }
	
	public function delete()
	{
		DB::table('users')->where('age', '>', 100)->delete();
		return json_encode(array('code'=>1));
	}
	
	public function update()
	{
		DB::table('users')
            ->where('id', 1)
            ->update(['age' => 11]);
		return json_encode(array('code'=>1));
	}
	
	public function add()
	{
		//插入一条
		/**
		DB::table('users')->insert(
			['name' => 'john@example.com', 'age' => rand(9,99),'status'=>1,'created_at'=>time(),'updated_at'=>time()]
		);
		*/
		
		$ret = DB::table('users')->insert([
			['name' => 'john1@example.com', 'age' => rand(9,99),'status'=>1,'created_at'=>time(),'updated_at'=>time()],
			['name' => 'john2@example.com', 'age' => rand(9,99),'status'=>1,'created_at'=>time(),'updated_at'=>time()]
		]);
		var_dump($ret);
		if(!$ret){
			//如果插入失败
			return json_encode(array('code'=>-1));
		}
		return json_encode(array('code'=>1));
	}
	
	
}